<!-- 物料/产品管理列表 -->
<template>
  <div class="container">
    <div class="content">
      <div>
        <a-space>
          <div style="display: flex">
            <a-space>
              <a-input-search
                v-model="numberVal"
                placeholder="请输入物料编码"
                search-button
              />
            </a-space>
          </div>
          <div style="display: flex">
            <a-space>
              <a-input-search
                v-model="nameVal"
                placeholder="请输入物料名称"
                search-button
              />
            </a-space>
          </div>
          <div>
            <a-space>
              <a-button type="outline" @click="reset">
                <template #icon> <icon-refresh /> </template> 重置
              </a-button>
            </a-space>
          </div>
        </a-space>
      </div>
      <a-divider />
      <div
        v-if="warningVal"
        :style="{
          marginTop: '10px',
        }"
      >
        <a-alert
          banner
          type="warning"
          :style="{
            cursor: 'pointer',
          }"
          >库存预警提醒：有
          <span
            :style="{
              color: 'red',
            }"
          >
            {{ waitJobCount }}
          </span>
          个物料的库存已低于安全库存量，请尽快处理！</a-alert
        >
      </div>
    </div>
    <a-divider />
    <div class="content">
      <tableTurn :tableData="getData" ref="tableRef">
        <a-table-column :width="80" align="center">
          <template #title> 序号 </template>
          <template #cell="{ rowIndex }">
            {{ rowIndex + 1 }}
          </template>
        </a-table-column>
        <a-table-column
          align="center"
          :width="200"
          title="物料编码"
          data-index="number"
        >
        </a-table-column>
        <a-table-column
          align="center"
          :width="150"
          title="物料名称"
          data-index="name"
          :ellipsis="true"
          :tooltip="{
            position: 'top',
          }"
        >
        </a-table-column>

        <a-table-column
          align="center"
          :width="150"
          title="规格型号"
          data-index="spec"
          :ellipsis="true"
          :tooltip="{
            position: 'top',
          }"
        >
        </a-table-column>
        <a-table-column
          align="center"
          data-index="basicName"
          :width="100"
          title="基本单位"
        >
        </a-table-column>

        <a-table-column
          align="center"
          :width="100"
          title="所属分类"
          data-index="materialName"
        >
        </a-table-column>

        <a-table-column align="center" :width="300" title="库存量">
          <a-table-column
            align="center"
            title="库存总量"
            data-index="totalStock"
            :width="100"
          >
          </a-table-column>
          <a-table-column
            align="center"
            title="占用数量"
            data-index="occupiedStock"
            :width="100"
          >
          </a-table-column>
          <a-table-column
            align="center"
            title="空闲数量"
            data-index="freeStock"
            :width="100"
          >
          </a-table-column>
          <a-table-column
            align="center"
            title="安全库存量"
            data-index="safetyStock"
            :width="100"
          >
          </a-table-column>
        </a-table-column>

        <a-table-column
          align="center"
          :width="200"
          title="备注"
          data-index="remark"
          :ellipsis="true"
          :tooltip="{
            position: 'top',
          }"
        >
        </a-table-column>
        <a-table-column align="center" :width="100" fixed="right" title="操作">
          <template #cell="{ record }">
            <a-tooltip content="查看详情">
              <IconFont
                type="icon-details-def"
                :size="26"
                :style="{
                  marginRight: '10px',
                  cursor: 'pointer',
                }"
                @click="goDetail(record)"
              />
            </a-tooltip>
          </template>
        </a-table-column>
      </tableTurn>
    </div>
    <materialAdd
      v-model:visible="modalVisible"
      @materialEmit="materialEmit"
    ></materialAdd>
  </div>
</template>

<script lang="ts" setup>
  import { computed, ref, reactive, onMounted, nextTick } from 'vue';
  import { useRouter } from 'vue-router';
  import dayjs from 'dayjs';
  import { GetOverviewList } from '@/api/inventory-management/inventory-query/inventory-qverview/index';
  import mitter from '@/utils/mitt';
  // import materialAdd from './components/material-add/index.vue';

  const router = useRouter();
  let tableRef = ref();
  let getMaterialId = ref();
  let numberVal = ref(''); // 物料编码搜索值
  let nameVal = ref(''); // 物料编码搜索值
  let waitJobCount = ref(0);
  let warningVal = ref(false);

  mitter.on('MaterialId', (val) => {
    getMaterialId.value = val;
    nextTick(() => {
      tableRef.value.loadData();
    });
  });

  // 获取列表数据
  const getData = async (pageData?: any) => {
    try {
      waitJobCount.value = 0;
      const data: any = {};
      if (numberVal.value !== '') data.code = numberVal.value;
      if (nameVal.value !== '') data.name = nameVal.value;
      if (getMaterialId.value !== '') data.materialId = getMaterialId.value;
      const res = await GetOverviewList({
        ...pageData,
        ...data,
      });
      if (getMaterialId.value) {
        res.content = res.content.filter(
          (i: any) => i.materialId === getMaterialId.value
        );
      }
      res.content.forEach((item) => {
        if (item.totalStock < item.safetyStock) {
          warningVal.value = true;
          waitJobCount.value += 1;
        }
      });
      return Promise.resolve(res);
    } catch (error: unknown) {
      console.log(error);
    }
  };
  let modalVisible = ref(false); // 新增弹出框
  let currType = ref(''); // 判断是新增主分类还是子分类
  const currForm = reactive({
    list: {},
  });

  // 点击搜索按钮
  const search = () => {
    tableRef.value.loadData();
  };

  // 点击重置按钮
  const reset = () => {
    numberVal.value = '';
    nameVal.value = '';
    getMaterialId.value = null;
    tableRef.value.loadData();
  };

  // 监听键盘回车，按了回车也会查询
  const keyDown = (e: any) => {
    if (e.keyCode === 13) {
      tableRef.value.loadData();
    }
  };

  onMounted(() => {
    window.addEventListener('keydown', keyDown); // 监听键盘事件
  });

  const goDetail = (val: number) => {
    router.push({
      name: 'LocationDetails',
      query: { res: JSON.stringify(val) },
    });
  };
</script>

<style scoped lang="less">
  .container {
    // padding: 0 20px 0px 20px;
    margin-bottom: 10px;

    // .content {
    //   padding: 10px 10px;
    // }
    .state-item {
      position: relative;
      left: 50%;
      transform: translateX(-50%);
    }
    .state-item-Y {
      width: 30px;
      height: 30px;
      font-size: 12px;
      border: 1px solid rgba(148, 191, 255, 1);
      background-color: rgba(232, 243, 255, 1);
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #3d7eff;
    }
    .state-item-N {
      color: #f53f3f;
      width: 30px;
      height: 30px;
      font-size: 12px;
      border: 1px solid rgba(251, 172, 163, 1);
      background-color: rgba(255, 236, 232, 1);
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>
